<?php
/***************************************************************************
 *
 *   copyright            : (C) 2005 Winds of Storm
 *
 *   $Id: transcripts.php,v 1.13 2007/11/08 20:45:24 amorgan Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

require("./config.php");
$full_header	= 1;
$body['class']	= 'row2';
require($libdir . "init." . $phpEx);
require($libdir . "rooms." . $phpEx);
require($libdir . "chat_functs." . $phpEx);
require($libdir . "login_code." . $phpEx);

function show_login($error) {
	global	$config;
	global	$phpEx;

	$roomlist	= getRoomList();

	$template = new Template($config['TPL_DIR']);
	$template->set_filenames(array('body' => 'transcript_query.tpl'));

	$template->assign_vars(array(
		'ROOMLIST'	=> $roomlist,
		'ERROR'		=> $error,
		'USERNAME'	=> $_REQUEST['username'],
		'PASSWORD'	=> $_REQUEST['password']
	));

	show_date_select(&$template, '1');
	show_date_select(&$template, '2');

	$template->pparse('body');
}

function show_date_select($template, $suffix) {
	$months	= array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

	$this_month	= date("M");
	$this_year	= date("Y");
	$this_date	= date("j");

	print("<!-- today is $this_month $this_date $this_year -->\n");
	foreach ($months as $m => $n) {
		$month	= $m+1;
		$sel	= '';
		if ($n == $this_month) {
			$sel	= " SELECTED";
		}
		$template->assign_block_vars("month$suffix", array(
			'VALUE'		=> $month,
			'NAME'		=> $n,
			'SELECTED'	=> $sel
		));
	}

	for ($i = 1; $i < 32; $i++) {
		$sel	= '';
		if ($i == $this_date) {
			$sel	= " SELECTED";
		}
		$template->assign_block_vars("day$suffix", array(
			'VALUE'		=> $i,
			'NAME'		=> $i,
			'SELECTED'	=> $sel
		));
	}

	for ($i = 2004; $i <= $this_year; $i++) {
		$sel	= '';
		if ($i == $this_year) {
			$sel	= " SELECTED";
		}
		$template->assign_block_vars("year$suffix", array(
			'VALUE'		=> $i,
			'NAME'		=> $i,
			'SELECTED'	=> $sel
		));
	}

	for ($i = 0; $i < 24; $i++) {
		$j	= str_pad($i, 2, "0", STR_PAD_LEFT);
		$template->assign_block_vars("hour$suffix", array(
			'VALUE'		=> $j,
			'NAME'		=> $j,
			'SELECTED'	=> ''
		));
	}

	for ($i = 0; $i < 60; $i++) {
		$j	= str_pad($i, 2, "0", STR_PAD_LEFT);
		$template->assign_block_vars("minute$suffix", array(
			'VALUE'		=> $j,
			'NAME'		=> $j,
			'SELECTED'	=> ''
		));
	}
}

//print("<!-- op == {$_REQUEST['op']}, roomlist = {$_REQUEST['roomlist']} -->\n");

if ($_REQUEST['op'] == "") {
	//print("<!-- showing login, empty op -->\n");
	show_login("");
	exit;
}

if ($_REQUEST['roomlist'] == '' && $_REQUEST['room_name'] == '') {
	//print("<!-- showing login, empty roomlist -->\n");
	show_login("Please select a valid room.");
	exit;
}

if ($_REQUEST['room_name'] != '') { 
	//print("<!-- getting room info for {$_REQUEST['room_name']}... -->\n");
	$room	= getRoomInfoByName($_REQUEST['room_name']);
	$rid	= $room['room_id'];
} else {
	//print("<!-- getting room info for {$_REQUEST['roomlist']}... -->\n");
	$room   = getRoomInfo($_REQUEST['roomlist']);
	$rid	= $_REQUEST['roomlist'];
}

if ($rid == '') {
	show_login("Please select a valid room.");
	exit;
}

//print("<!-- checking authorization... rid = $rid-->\n");
auth_login($room);
//print("<!-- after authorization... -->\n");

$start	= "'{$_REQUEST['year1']}-{$_REQUEST['month1']}-{$_REQUEST['day1']} {$_REQUEST['hour1']}:{$_REQUEST['minute1']}:00'";
$end	= "'{$_REQUEST['year2']}-{$_REQUEST['month2']}-{$_REQUEST['day2']} {$_REQUEST['hour2']}:{$_REQUEST['minute2']}:59'";

$sql	= "SELECT
			*,DATE_FORMAT(event_date, '%m/%d %H:%i') AS event_date_fmt
		FROM
			{$dbprefix}events
		WHERE
			room_id_to=$rid AND
			(UNIX_TIMESTAMP(event_date) > UNIX_TIMESTAMP($start)) AND
			(UNIX_TIMESTAMP(event_date) < UNIX_TIMESTAMP($end)) AND
			event_chat_flags != 4
		ORDER BY
			event_id";
$str_sql = ereg_replace("\n", " ", $sql);
$str_sql = ereg_replace("\t", "", $str_sql);

print("<!-- SQL: $str_sql -->\n");

if (!($res = $db->sql_query($sql))) {
	$err	= $db->sql_error();
	showFatalError("<B>Error #{$err['code']}: {$err['message']}</b>: $str_sql");
}

$template = new Template($config['TPL_DIR']);
$template->set_filenames(array('body' => 'transcript_body.tpl'));

$vars	= array();
while (list($k, $v) = each($room)) {
	$vars[strtoupper($k)] = $v;
}
$vars['DATE1']	= stripslashes($start);
$vars['DATE2']	= stripslashes($end);
$template->assign_vars($vars);

while ($row = $db->sql_fetchrow($result)) {
	print("<!-- id = {$row['event_id']} -->");
	if ($_REQUEST['show_timestamps'] == 'on') {
		//print("[" . $row['event_date_fmt'] . "] ");
		$date_fmt	= "[" . $row['event_date_fmt'] . "] ";
	}
	//print(stripslashes($row['event_text']) . "</i></b></font><br />\n");

	$template->assign_block_vars('events', array(
		'EVENT_ID'	=> $row['event_id'],
		'EVENT_DATE'	=> $date_fmt,
		'EVENT_TEXT'	=> stripslashes($row['event_text'])
	));
}

$template->pparse('body');
?>
